{{define "admin.distribute-role"}}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>分配角色</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/statics/third-pkg/layui/css/layui.css">
  <link rel="stylesheet" href="/statics/admin/css/public.css" media="all">
  <style>
    body {
      background-color: #ffffff;
    }
  </style>
</head>

<body>
<div class="layui-form layuimini-form" style="margin-top: 20px;">
  <input type="hidden" name="id" value="{{ .model.ID }}">
  <div class="layui-form-item">
    <label class="layui-form-label required">角色</label>
    <div class="layui-input-block">
      <div id="roles" class="xm-select"></div>
    </div>
  </div>
  <div class="layui-form-item" style="margin-top: 40px;">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
    </div>
  </div>
</div>
<script src="/statics/third-pkg/layui/layui.js"></script>
<script src="/statics/admin/js/lay-config.js?v=2.0" charset="utf-8"></script>

<script>

  layui.use(['form','request','layer','miniTab','common',"xmSelect"], function() {
    var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            common=layui.common,
            xmSelect=layui.xmSelect,
            request=layui.request;

    var role = xmSelect.render({
      el: '#roles',
      radio: true,
      name: 'role',
      layVerify: 'required',
      layVerType: 'msg',
      filterable: true,
      height: 'auto',
      clickClose:true,
      paging: true,
      // pageEmptyShow: false,
      //远程分页
      pageRemote: true,
      theme: {
        color: '#0081ff',
      },
      data: [
      ],
      remoteMethod: function(val, cb, show, pageIndex){
        //val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
        request.get('admin/roles/select-get-list?limit=10&page='+pageIndex).then(function (response) {
          if (response.data.code == 0) {
            //这里是success的处理
            var res = response.data;
            var data=[];
            for (let dataKey in res.data) {
              data.push({
                name:res.data[dataKey].name,
                value:res.data[dataKey].value,
                selected:"{{ .roles }}"==res.data[dataKey].value ? true :false
              })
            }
            //回调需要两个参数, 第一个: 数据数组, 第二个: 总页码
            cb(data,response.data.pagination.page_count)
          } else {
            layer.msg(response.data.msg);
            cb([], 0);
          }
        }).catch(function (error) {
          //这里是error的处理
          cb([], 0);
          layer.msg('获取角色失败');
        })
      }
    })
    //监听提交
    form.on('submit(saveBtn)', function(data) {
      var submitData={
        ...data.field,
        id:Number.parseInt(data.field['id']),
      }
      request.post('admin/admin/distribute-role',submitData).then(function (response) {
        if(response.status==204){
          layer.msg('分配角色成功',{
            time:1000
          },function (){
            localStorage.setItem("admin-add",common.generateUUID())
            miniTab.deleteCurrentByIframe();
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);

            window.location.href="/admin/admin/list";
          })
        }else{
          layer.msg(response.data.msg);
        }
      }).catch(function (error) {
        console.log(error)
        layer.msg('设置角色失败');
      });

    });

  });
</script>
</body>

</html>
{{ end }}